package com.ywu.standard.gateway.redis;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RedisUtil
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/2/29 22:04
 * @Version 1.0
 **/
@Component
public class RedisUtil {
    public static final String GATEWAY_ROUTES = "gateway:routes";

    @Autowired
    private RedisTemplate redisTemplate;

    public List<RouteDefinition> getAllGatewayRoutes() {
        List<RouteDefinition> routeDefinitions = new ArrayList<>();
        redisTemplate.opsForHash().values(GATEWAY_ROUTES).stream().forEach(routeDefinition -> {
            routeDefinitions.add(JSON.parseObject(routeDefinition.toString(), RouteDefinition.class));
        });
        return routeDefinitions;
    }
}
